package cn.jbolt.ec.order.handler;

import cn.jbolt.common.model.*;
import cn.jbolt.ec.retailer.RetailerService;
import com.jfinal.aop.Inject;

/**
 * B型订单处理类 （简易单）
 */
public class TypeB {
    @Inject
    RetailerService retailerService;



    public int handle(int client_id,int retailer_id,int price){

        /*
        生成新订单
         */

        BecRetailerOrder becRetailerOrder = new BecRetailerOrder();
        BecMember member = new BecMember().dao().findByIdLoadColumns(client_id,"mobile,nickname");
        if(member==null){
            return -1;
        }

        becRetailerOrder.setAddress("到店消费")
                .setMobile(" "+member.getMobile())
                .setConsignee(" "+member.getNickname()).setStatus(5)
                .setType(2).setClientId(client_id)
                .setRetailerId(retailer_id).setDiscountAmount(0);


        String orderids = "j-"+System.currentTimeMillis()+'-'+client_id+'-'+retailer_id;
        becRetailerOrder.setOrderId(orderids);
        if(!becRetailerOrder.save()) return -2; // 订单生成失败


        BecRetailer becRetailer = new BecRetailer().dao().findByIdLoadColumns(retailer_id,
                "id,discount,spreader_credit,platform_commission");
        if(becRetailer==null) return -3;
        long amount_total = price;



        Integer discount = becRetailer.getDiscount();
        if(discount==null) discount = 0;

        discount = (int)price*discount/100;



        int amount_paid = (int)price - discount;


        becRetailerOrder.setAmountPaid((long)amount_paid)
                .setAmountTotal(amount_total)
                .setOrderTime(System.currentTimeMillis())
                .setCommission((int)(amount_total*becRetailer.getPlatformCommission()/100))
                .setCredit((int)(amount_total*becRetailer.getSpreaderCredit()/100))
                .setDiscountAmount(discount);

        if(!becRetailerOrder.update()) return -4;

        return becRetailerOrder.getId();



    }

}
